package com.sxyc.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

public class DateUtil {

	public static final String simformat = "yyyy-MM-dd HH:mm:ss";
	public static final String simformat1 = "yyyyMMddHHmmss";
	public static final String simformat2 = "yyyy-MM-dd";
	public static final String simformat3 = "MM-dd";

	public static String getcurrentDate() {
		SimpleDateFormat format = new SimpleDateFormat(simformat2);
		return format.format(new Date());
	}
	
	@SuppressWarnings("static-access")
	public static String getTomorrowDate() {
		SimpleDateFormat format = new SimpleDateFormat(simformat2);
		
		 Calendar calendar = new GregorianCalendar();
		 calendar.setTime(new Date());
		 calendar.add(calendar.DATE,1);//把日期往后增加一天.整数往后推,负数往前移动 

		return format.format(calendar.getTime());
	}
	
	//获取指定时间的下一天
	public static String getTomorrowDate(String date){
		SimpleDateFormat format = new SimpleDateFormat(simformat2);
		try {
			long timestramp = format.parse(date).getTime();
			timestramp += 1000*3600*24;
			return format.format(timestramp);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			return null;
		}
	}
	
	//获取指定时间 的 yyyy-MM-dd
	public static String getDate(String date) {
		SimpleDateFormat format = new SimpleDateFormat(simformat2);
		return format.format(date);
	}
	
	//获取指定时间 的 yyyy-MM-dd
	public static String getDate(long date) {
		SimpleDateFormat format = new SimpleDateFormat(simformat2);
		return format.format(date);
	}
	
	//获取当前时间的  yyyy-MM-dd HH:mm:ss
	public static String getcurrentTime() {
		SimpleDateFormat format = new SimpleDateFormat(simformat);
		return format.format(new Date());
	}

	
	public static boolean compareDate(String starttime, String endtime) {
		SimpleDateFormat format = new SimpleDateFormat(simformat);
		long start = 0;
		long end = 0;
		try {
			start = format.parse(starttime).getTime();
			end = format.parse(endtime).getTime();

		} catch (ParseException e) {
			e.printStackTrace();
			return false;
		}

		if (start > end) {
			return true;
		}

		return false;
	}

	public static String getTimeByString(long time) {
		SimpleDateFormat format = new SimpleDateFormat(simformat);
		return format.format(new Date(time));
	}
	
	public static long getTimeByLong(String time) {
		SimpleDateFormat format = new SimpleDateFormat(simformat);
		try {
			return format.parse(time).getTime();
		} catch (ParseException e) {
			e.printStackTrace();
			return 0;
		}
	}

	public static long getDateByLong(String time) {
		SimpleDateFormat format = new SimpleDateFormat(simformat2);
		try {
			return format.parse(time).getTime();
		} catch (ParseException e) {
			e.printStackTrace();
			return 0;
		}
	}
	
	public static Date getTimeByDate(String time) {
		SimpleDateFormat format = new SimpleDateFormat(simformat);
		try {
			return format.parse(time);
		} catch (ParseException e) {
			e.printStackTrace();
			return new Date();
		}
	}
	
	public static void main(String[] args) {
		System.out.println(getDateByLong("2017-3-24"));
	}
}
